package cn.wolfcode.web.controller;

import cn.wolfcode.transfer.domain.Permission;
import cn.wolfcode.transfer.qo.JsonResult;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.IPermissionService;
import cn.wolfcode.transfer.util.RequiredPermission;
import com.alibaba.fastjson.JSONReader;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService PermissionService;

    @RequiredPermission(name = "权限页面", expression = "Permission:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Permission> pageInfo = PermissionService.getPageInfo(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";

    }

    @RequiredPermission(name = "权限删除", expression = "Permission:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            PermissionService.deleteByPrimaryKey(id);
        }
        return "redirect:/permission/list";
    }

    @RequiredPermission(name = "加载权限", expression = "Permission:reload")
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {
            PermissionService.reload();
            return new JsonResult();
    }
}
